import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  HttpCode,
  HttpStatus,
  UseGuards,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
  ApiBody,
} from '@nestjs/swagger';
import { GroupBuyService } from './group-buy.service';
import type { GroupBuy } from './group-buy.service';

@ApiTags('团购活动')
@Controller('group-buys')
export class GroupBuyController {
  constructor(private readonly groupBuyService: GroupBuyService) {}

  @ApiOperation({ summary: '获取团购列表' })
  @ApiResponse({ status: 200, description: '成功获取团购列表' })
  @Get()
  findAll(): GroupBuy[] {
    return this.groupBuyService.findAll();
  }

  @ApiOperation({ summary: '根据ID获取团购详情' })
  @ApiResponse({ status: 200, description: '成功获取团购详情' })
  @ApiResponse({ status: 404, description: '团购不存在' })
  @Get(':id')
  findOne(@Param('id') id: string): GroupBuy {
    const groupBuy = this.groupBuyService.findById(id);
    if (!groupBuy) {
      throw new Error('Group buy not found');
    }
    return groupBuy;
  }

  @ApiOperation({ summary: '创建团购' })
  @ApiResponse({ status: 201, description: '团购创建成功' })
  @ApiBearerAuth()
  @ApiBody({ type: () => Object, description: '团购数据' })
  @Post()
  @HttpCode(HttpStatus.CREATED)
  create(@Body() groupBuyData: Omit<GroupBuy, 'id' | 'currentParticipants' | 'participants' | 'status' | 'createdAt' | 'updatedAt'>): GroupBuy {
    return this.groupBuyService.create(groupBuyData);
  }

  @ApiOperation({ summary: '更新团购' })
  @ApiResponse({ status: 200, description: '团购更新成功' })
  @ApiResponse({ status: 404, description: '团购不存在' })
  @ApiBearerAuth()
  @ApiBody({ type: () => Object, description: '团购更新数据' })
  @Put(':id')
  update(
    @Param('id') id: string,
    @Body() groupBuyData: Partial<Omit<GroupBuy, 'id' | 'createdAt' | 'participants' | 'currentParticipants'>>,
  ): GroupBuy {
    const result = this.groupBuyService.update(id, groupBuyData);
    if (!result) {
      throw new Error('Group buy not found');
    }
    return result;
  }

  @ApiOperation({ summary: '删除团购' })
  @ApiResponse({ status: 204, description: '团购删除成功' })
  @ApiResponse({ status: 404, description: '团购不存在' })
  @ApiBearerAuth()
  @Delete(':id')
  @HttpCode(HttpStatus.NO_CONTENT)
  remove(@Param('id') id: string): void {
    const result = this.groupBuyService.delete(id);
    if (!result) {
      throw new Error('Group buy not found');
    }
  }

  @ApiOperation({ summary: '参与团购' })
  @ApiResponse({ status: 200, description: '成功参与团购' })
  @ApiResponse({ status: 400, description: '无法参与团购' })
  @ApiResponse({ status: 404, description: '团购不存在' })
  @ApiBearerAuth()
  @ApiBody({ schema: { type: 'object', properties: { userId: { type: 'string' } } } })
  @Post(':id/join')
  joinGroupBuy(
    @Param('id') id: string,
    @Body('userId') userId: string,
  ): GroupBuy {
    const result = this.groupBuyService.joinGroupBuy(id, userId);
    if (!result) {
      throw new Error('Failed to join group buy');
    }
    return result;
  }

  @ApiOperation({ summary: '启动团购' })
  @ApiResponse({ status: 200, description: '团购启动成功' })
  @ApiResponse({ status: 400, description: '无法启动团购' })
  @ApiResponse({ status: 404, description: '团购不存在' })
  @ApiBearerAuth()
  @Post(':id/start')
  startGroupBuy(@Param('id') id: string): GroupBuy {
    const result = this.groupBuyService.startGroupBuy(id);
    if (!result) {
      throw new Error('Failed to start group buy');
    }
    return result;
  }

  @ApiOperation({ summary: '获取用户参与的团购' })
  @ApiResponse({ status: 200, description: '成功获取用户团购' })
  @ApiBearerAuth()
  @Get('user/:userId')
  getUserGroupBuys(@Param('userId') userId: string): GroupBuy[] {
    return this.groupBuyService.getUserGroupBuys(userId);
  }
}